package com.glodon.paas.document.biz.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.glodon.paas.document.api.bean.ProjectMemberAttendees;
import com.glodon.paas.document.biz.ProjectBiz;
import com.glodon.paas.document.dao.FileDao;
import com.glodon.paas.document.dao.InvitationDao;
import com.glodon.paas.document.dao.ProjectDao;
import com.glodon.paas.document.dao.ProjectMemberAttendeesDao;
import com.glodon.paas.document.dao.ProjectMemberDao;
import com.glodon.paas.document.dao.ProjectResourceDao;
import com.glodon.paas.document.dao.ProjectRoleDao;
import com.glodon.paas.document.dao.domain.FileInfoEntity;
import com.glodon.paas.document.dao.domain.InvitationEntity;
import com.glodon.paas.document.dao.domain.ProjectInfoEntity;
import com.glodon.paas.document.dao.domain.ProjectMemberAttendeesEntity;
import com.glodon.paas.document.dao.domain.ProjectMemberEntity;
import com.glodon.paas.document.dao.domain.ProjectResourceEntity;
import com.glodon.paas.document.dao.domain.ProjectRoleEntity;
import com.glodon.paas.exception.PaasServerExceptionFactory;

@Repository("projectBiz")
public class ProjectBizImpl implements ProjectBiz {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectBizImpl.class);
    @Autowired
    private FileDao fileDao;
    @Autowired
    private ProjectDao          projectDao;
    @Autowired
    private ProjectRoleDao      projectRoleDao;
    @Autowired
    private ProjectMemberDao    projectMemberDao;
    @Autowired
    private InvitationDao       invitationDao;
    @Autowired
    private ProjectResourceDao projectResourceDao;
    @Autowired
    private ProjectMemberAttendeesDao attendeesDao;
    @Override
    public String createProject(ProjectInfoEntity project, FileInfoEntity folder, ProjectRoleEntity role, ProjectRoleEntity defaultRole,
                                ProjectMemberEntity member,ProjectMemberAttendeesEntity attendee, ProjectResourceEntity ownerRoleResource, ProjectResourceEntity defaultRoleResource) {
        projectDao.insert(project);
        fileDao.insert(folder);
        projectMemberDao.insert(member);
        attendeesDao.insert(attendee);
        projectRoleDao.insert(role);
        projectRoleDao.insert(defaultRole);
        projectResourceDao.insert(ownerRoleResource);
        projectResourceDao.insert(defaultRoleResource);
        return project.getId();
    }

    @Override
    public boolean deleteProject(String id) {
        boolean result = false;
        try {
            result = projectDao.delete(id) > 0;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw PaasServerExceptionFactory.dbException(e.getLocalizedMessage(), e);
        }
        return result;
    }

    @Override
    public boolean updateProject(ProjectInfoEntity entity) {
        boolean result = false;
        try {
            result = projectDao.updateProject(entity) > 0;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw PaasServerExceptionFactory.dbException(e.getLocalizedMessage(), e);
        }
        return result;
    }

    @Override
    public List<ProjectInfoEntity> listProjects(String ownerId) {
        return projectDao.getProjects(ownerId);
    }

    @Override
    public String createRole(ProjectRoleEntity role, ProjectResourceEntity resource) {
        projectRoleDao.insert(role);
        projectResourceDao.insert(resource);
        return role.getId();
    }

    @Override
    public List<ProjectRoleEntity> listRoles(String projectId) {
        return projectRoleDao.getRoles(projectId);
    }

    @Override
    public ProjectRoleEntity updateRole(String userId, String roleId, String name) {
        ProjectRoleEntity dbRole = projectRoleDao.get(roleId);
        dbRole.setRoleName(name);
        dbRole.setUpdator(userId);
        dbRole.setUpdateTime(new Date());
        projectRoleDao.updateRole(dbRole);
        return dbRole;
    }

    @Override
    public boolean deleteRole(String roleId) {
        boolean result = false;
        try {
            result = projectRoleDao.delete(roleId) > 0;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw PaasServerExceptionFactory.dbException(e.getLocalizedMessage(), e);
        }
        return result;
    }

    @Override
    public ProjectInfoEntity getProjectByName(String ownerId, String name) {
        return projectDao.getProjectByName(ownerId, name);
    }

    @Override
    public String insertMember(ProjectMemberEntity entity) {
        String result = "";
        try {
            result =projectMemberDao.insert(entity);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw PaasServerExceptionFactory.dbException(e.getLocalizedMessage(), e);
        }
        return result;
    }

    @Override
    public boolean updateMemberStatus(String memberId, int status) {
        return projectMemberDao.updateMemberStatus(memberId, status);
    }

    @Override
    public ProjectInfoEntity getProjectById(String projectId) {
        return projectDao.get(projectId);
    }

    @Override
    public boolean insertInvitation(InvitationEntity entity) {
        invitationDao.insert(entity);
        return true;
    }

    @Override
    public boolean confirmInvitation(String userId, String token) {
        return projectMemberDao.acceptMember(userId, token);
    }

    @Override
    public List<ProjectInfoEntity> getProjectByIds(List<String> ids) {
        return projectDao.getProjects(ids);
    }

    @Override
    public List<ProjectMemberEntity> getMembers(String projectId, String roleId) {
        return projectMemberDao.getMembers(projectId, roleId);
    }

    @Override
    public ProjectMemberEntity getConfirmedMember(String userId, String token) {
        InvitationEntity entity = invitationDao.getInvitation(userId, token);
        if (entity != null) {
            return projectMemberDao.get(entity.getMemberId());
        }
        return null;
    }

    @Override
    public boolean removeMember(String memberId,boolean deleteProjectMember) {
        if(deleteProjectMember){
            projectMemberDao.delete(memberId);
        }
        attendeesDao.removeAttedees(memberId);
        return  true;
    }

    @Override
    public ProjectMemberEntity getMember(String memberId) {
        return projectMemberDao.get(memberId);
    }

    @Override
    public int setDefaultRoleForMember(String projectId, String roleId) {
        return projectMemberDao.updateDefaultRole(projectId, roleId);
    }

    @Override
    public ProjectRoleEntity getDefaultRole(String projectId) {
        return projectRoleDao.getDefaultRole(projectId);
    }

    @Override
    public boolean updateRoleOfMember(String memberId, String projectId, String oldRoleId, String newRoleId) {
        return projectMemberDao.updateRoleOfMember(memberId, projectId, oldRoleId, newRoleId) >0;
    }

    @Override
    public boolean checkSameRole(ProjectRoleEntity role) {
        return projectRoleDao.checkSameRole(role);
    }

    @Override
    public boolean updateInvitationStatus(String email, String token) {
        return invitationDao.updateInvitationStatus(email,token)>0;
    }

    @Override
    public ProjectMemberEntity getMember(String projectId, String roleId, String account,String orgId) {
        return projectMemberDao.getMember(projectId, roleId,account,orgId);
    }

    @Override
    public ProjectRoleEntity getRole(String roleId) {
        return projectRoleDao.get(roleId);
    }

    @Override
    public List<ProjectMemberEntity> getAddedOrgs(String projectId, String roleId) {
        return projectMemberDao.getAddedOrgs(projectId, roleId);
    }

    public void setProjectDao(ProjectDao projectDao) {
        this.projectDao = projectDao;
    }

    public void setProjectRoleDao(ProjectRoleDao projectRoleDao) {
        this.projectRoleDao = projectRoleDao;
    }

    public void setProjectMemberDao(ProjectMemberDao projectMemberDao) {
        this.projectMemberDao = projectMemberDao;
    }

    public void setInvitationDao(InvitationDao invitationDao) {
        this.invitationDao = invitationDao;
    }

    public void setProjectResourceDao(ProjectResourceDao projectResourceDao) {
        this.projectResourceDao = projectResourceDao;
    }
    
    public void setFileDao(FileDao fileDao) {
        this.fileDao = fileDao;
    }

    @Override
    public String insertAttendees(ProjectMemberAttendeesEntity entity) {
        return attendeesDao.insert(entity);
    }

    @Override
    public List<ProjectMemberAttendees> getAttendees(String projectMemberId) {
        return ProjectMemberAttendeesEntity.getDtos(attendeesDao.getAttendees(projectMemberId));
    }

    @Override
    public ProjectMemberAttendeesEntity getAttendeeById(String id) {
        return attendeesDao.get(id);
    }

    @Override
    public boolean setInvitaionInvalid(String id) {
        return invitationDao.setInvitaionInvalid(id)>0;
    }

    @Override
    public ProjectMemberAttendeesEntity getAttendeeByUIDandPMID(String userId, String projectMemberId) {
        return attendeesDao.getAttendeeByUIDandPMID(userId,projectMemberId);
    }

    @Override
    public void updateAttendeeInvitation(String attendeeId, String invitationId) {
        attendeesDao.updateAttendeeInvitation(attendeeId,invitationId);
    }
}
